package com.example.opengl.camera

import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Route
import com.android.baselibrary.base.BaseActivity
import com.android.baselibrary.utils.getScreenHeight
import com.android.baselibrary.utils.getScreenWidth
import com.android.baselibrary.utils.getViewModelOfActivity
import com.example.opengl.R
import com.example.opengl.camera.dialog.FilterBean
import com.example.opengl.camera.dialog.PopFilterView
import com.example.opengl.camera.filter.FilterType
import com.example.opengl.camera.mediarecord.MediaRecordModel
import com.example.opengl.databinding.ActivityCameraPreviewBinding
import com.example.opengl.main.OpenGLPath
import java.io.File
import java.io.FileDescriptor

/**
 * @author: Mr.You
 * @create: 2023-07-18 16:08
 * @description: OpenGL渲染Camera画面
 * 视频录制: 方案1 使用MediaRecord  方法2 使用MediaCodec
 **/
@Route(path = OpenGLPath.OPENGL_PREVIEW_CAMERA)
class CameraPreviewActivity : BaseActivity<ActivityCameraPreviewBinding>(), PopFilterView.FilterSelectListener {
    override fun getLayoutId(): Int = R.layout.activity_camera_preview

    private val popFilterView: PopFilterView by lazy {
        PopFilterView(this)
    }

    @RequiresApi(Build.VERSION_CODES.N)
    override fun initView() {
        popFilterView.setFilterSelectListener(this)

        val viewModel = getViewModelOfActivity(CameraViewModel::class.java)
        //  ViewModelProvider(this,ViewModelProvider.NewInstanceFactory())[CameraViewModel::class.java]


        mBinding.iCamera = viewModel






        viewModel.switchCameraLiveData.observe(this) {
            mBinding.surface.switchCamera()
        }
        viewModel.addFilterLiveData.observe(this) {
            if (popFilterView.isShowing) return@observe

            popFilterView.show(mBinding.root)
        }
        viewModel.addBeautyFilterLiveData.observe(this) {
            mBinding.surface.setBeautyFilter()
        }
        viewModel.recordVideoLiveData.observe(this) {
            if (it == null) return@observe
            mBinding.surface.setRecordVideo(it)
        }
    }

    override fun initListener() {

    }

    override fun filterSelect(filterBean: FilterBean?, filterType: FilterType?) {
        filterType?.let { mBinding.surface.switchFilter(it) }
    }

    override fun filterDimss() {

    }

    override fun recoverFilter() {

    }
}